trigger CountAttendee on NationalAttendee__c (after delete,after insert,after update){

    Profile p  = [select id from profile where name=: 'System Administrator' or name =: '系统管理员'];
    if(userInfo.getProfileId() != p.id){

    List<CityNameList__c> cnl=new List<CityNameList__c>();
    list<regionnamelist__c> rgl=new list<regionnamelist__c>();
    List<NationalAttendee__c> nal=new List<NationalAttendee__c>();
    List<HCPEducation__c> hcpl=new List<HCPEducation__c>();
    List<RecordType> rt=new List<recordtype>();
    list<joinpeople__c> jpl=new list<joinpeople__c>();
    joinpeople__c jp;
    id salonid;
    id psid;
    id hsalonid;
    id hpsid;
    id hnaid;
    rt=[select id,developername,sobjecttype from recordtype where (sobjectType=:'NationalAttendee__c' and (developername=:'saloon' or developername=:'PocketSession')) or 
            (sobjecttype=:'hcpeducation__c' and (developername=:'SalonPlan' or developername=:'PocketSession' or developername=:'nationalHCPEducation'))
        ];
    
    
    for(recordtype r:rt){
        if(r.developername=='PocketSession' && r.sobjecttype=='NationalAttendee__c'){
            psid=r.id;
        }
        if(r.developername=='saloon' && r.sobjecttype=='NationalAttendee__c'){
            salonid=r.id;
        }
        if(r.developername=='SalonPlan' && r.sobjecttype=='Hcpeducation__c'){
            hsalonid=r.id;
        }
        if(r.developername=='PocketSession' && r.sobjecttype=='Hcpeducation__c'){
            hpsid=r.id;
        }
        if(r.developername=='nationalHCPEducation' && r.sobjecttype=='Hcpeducation__c'){
            hnaid=r.id;
        }
    }
   
    if(system.trigger.isinsert||system.trigger.isupdate){
    list<id> hcpeduid=new list<id>();
    for(nationalattendee__c n:trigger.new){
        hcpeduid.add(n.hcpeducation__c);
    }
    hcpeducation__c hcpedu=new hcpeducation__c();
    try{
    hcpedu=[select TotalNumberoftargetHCP__c,id,totalarrived__c,recordtypeid from hcpeducation__c where id in:hcpeduid];//=:trigger.new[0].hcpeducation__c];
     }catch(exception e){
    list<id> nal123=new list<id>();
        for(nationalattendee__c na:trigger.new){
        if(na.hcpeducation__c!=null){
            nal123.add(na.hcpeducation__c);
            }
        }
        system.assert(false,'记录ID为：'+trigger.new+'  活动ID为：'+nal123+'  错误信息为：'+e);
    }
     jpl=[select id,CityNameList__c,NationalHCPEducation__c,regionNameListId__c from joinpeople__c where CityNameList__r.regionNameList__r.HCPEducation__c in:hcpeduid and User__c=:userinfo.getuserid()];
//     system.assert(false,trigger.new[0].hcpeducation__c);
    if(jpl.size()>0){
        jp=jpl[0];
    }

    if(hcpedu.recordtypeid==hpsid||hcpedu.recordtypeid==hsalonid){
    //system.assert(false,rt[0].developername);
    if(hcpedu.recordtypeid==hsalonid){
     //============count salon attendee========
    list<nationalattendee__c> nanum=[select id,isattended__c from nationalattendee__c where hcpeducation__c=:trigger.new[0].hcpeducation__c];
    
    hcpedu.LocalActivityAttends__c=nanum.size();
    hcpedu.TotalNumberoftargetHCP__c=nanum.size();
    update hcpedu;
     //============end of count salon attendee code==============
    }
    }
    else if(hcpedu.recordtypeid==hnaid){
    //===========count national attendee===========
      cnl=[select id,PlannedAttendee__c,Attendee__c from CityNameList__c where Id=:jp.CityNameList__c]; //把对应的城市名单找出来，提取ID,PLANNEDATTENDEE
      rgl=[select id,regionAttendee__c from regionnamelist__c where id=:jp.regionnamelistid__c];
      regionnamelist__c rg;
      CityNameList__c cn;
      if(rgl.size()>0){
            rg=rgl[0];
       }
       if(cnl.size()>0){
            cn=cnl[0];
       }
       nal=[select id,IsAttended__c,citynamelists__c,regionnamelist__c from NationalAttendee__c where (CityNameLists__r.id=:cn.id or regionnamelist__r.id=:rg.id)];//找出所有在这名单下的HCP
          if(nal.size()>0){
          integer i=0;
          integer r=0;
          for(nationalattendee__c n:nal){
              if(n.citynamelists__c==trigger.new[0].citynamelists__c){
                  i++;
              }
              if(n.regionnamelist__c==trigger.new[0].regionnamelist__c){
                  r++;
              }
          }
         rg.regionattendee__c=r;
         cn.PlannedAttendee__c=i;
         // List<NationalAttendee__c> attl=[select IsAttended__c from NationalAttendee__c where IsAttended__c=:true and CityNameLists__c=:cn.id];
         cn.Attendee__c=i;
          }
           update rg;
          update cn;
         
         
        
         
         list<nationalattendee__c> nalll=[select id from NationalAttendee__c where IsAttended__c=:true and HCPEducation__c=:trigger.new[0].hcpeducation__c];
         list<hcpeducation__c>  nhcpl=[select id from HCPEducation__c where id=:trigger.new[0].HCPEducation__c];
                         if(nhcpl.size() > 0){
                         HCPEducation__c hcp=nhcpl[0];         
                         hcp.LocalActivityAttends__c=nalll.size();
                         update hcp;
                         }
    
    //======================================
    }
    
    }

    
      if(System.trigger.isdelete&&trigger.old.size()==1){
       hcpl=[select localactivityattends__c,TotalNumberoftargetHCP__c,id,totalarrived__c,recordtypeid,date__c from hcpeducation__c where id=:trigger.old[0].hcpeducation__c];
    jpl=[select id,CityNameList__c,NationalHCPEducation__c,regionNameListId__c from joinpeople__c where CityNameList__r.regionNameList__r.HCPEducation__c=:trigger.old[0].hcpeducation__c and User__c=:userinfo.getuserid()];
//     system.assert(false,trigger.new[0].hcpeducation__c);
    if(jpl.size()>0){
        jp=jpl[0];
    }
      for (NationalAttendee__c na:trigger.old){

//===========below is for pocket session/saloon============

   if(hcpl[0].recordtypeid==hpsid||hcpl[0].recordtypeid==hsalonid){
   //   hcpl=[select localactivityattends__c,TotalNumberoftargetHCP__c,id,date__c from HCPEducation__c where id=:na.HCPEducation__c];
   
     if(hcpl.size()>0){
        for (NationalAttendee__c nat : Trigger.old)
        {
            if((nat.ownerid != UserInfo.getUserId())&&(UserInfo.getProfileId()!= p.id)){
                nat.addError('您无权删除该医护人员!');
            }
            if(((nat.inplan__c==true && hcpl[0].date__c<= Date.Today()&&nat.recordtypeid==psid)|| ( date.today()>date.newInstance(hcpl[0].date__c.year(),hcpl[0].date__c.month()+1,5) ))&&(UserInfo.getProfileId()!= p.id)){         //(hcpl[0].date__C.month()<date.today().month())){
          //      nat.addError('该医护人员不能被删除!');
            }
         }
    }
      
           if(na.inplan__c==true){
           hcpl[0].totalnumberoftargethcp__c--;
           }
           if(na.isattended__c==true){
            hcpl[0].localactivityattends__c--;
           }
           try{
           update hcpl[0];
           }catch(exception e){}
          
      /*    else{
       if(hcpl.size()>0){
           HCPEducation__c hcp=hcpl[0];
          nal=[select id,IsAttended__c from NationalAttendee__c where HCPEducation__r.id=:hcp.id and inplan__c=:true];
          if(nal.size()>0){
           Integer i=nal.size();
           hcp.TotalNumberoftargetHCP__c=i;
           }
           List<NationalAttendee__c> attl=[select IsAttended__c,id from NationalAttendee__c where IsAttended__c=:true and HCPEducation__r.id=:hcp.id];
           hcp.LocalActivityAttends__c=attl.size();
           try{
           update hcp;  
           }catch(exception e){}
       }
   }*/
   }

//==========end of pocket session/saloon code========
   
   
   
//============below is for National hcp education activity===========   
     else if(hcpl[0].recordtypeid==hnaid){
     
      cnl=[select attendee__c,id,PlannedAttendee__c from CityNameList__c where Id=:jp.CityNameList__c]; //把对应的城市名单找出来，提取ID,PLANNEDATTENDEE
       rgl=[select id,regionAttendee__c from regionnamelist__c where id=:jp.regionNameListId__c];
       hcpl=[select localactivityattends__c,TotalNumberoftargetHCP__c,id,date__c from HCPEducation__c where id=:na.HCPEducation__c];
       
          
          
          if(cnl[0].attendee__c==1){
           cnl[0].attendee__c--;
           update cnl[0];
           rgl[0].regionattendee__c--;
           update rgl[0];
           hcpl[0].localactivityattends__c--;
           update hcpl[0];
          }
      
      else{
      if (cnl.size()>0){
       CityNameList__c cn=cnl[0];
       
       nal=[select id,citynamelists__c,regionnamelist__c from NationalAttendee__c where (CityNameLists__c=:cn.id or regionnamelist__c=:rgl[0].id or hcpeducation__c=:hcpl[0].id)];//找出所有在这名单下的HCP
         if(nal.size()>0){
           integer r=0;
           integer h=nal.size();
           Integer i=0;
           
           for(nationalattendee__c n:nal){
               if(n.citynamelists__c==trigger.old[0].citynamelists__c){
                   i++;
               }
               if(n.regionnamelist__c==trigger.old[0].regionnamelist__c){
                   r++;
               }
           }
            cn.Attendee__c=i;
            rgl[0].regionattendee__c=r;
            hcpl[0].localactivityattends__c=h;
             }   
            update cn;
            update rgl[0];
            update hcpl[0];
         }
         }
         
         
         
         
         
         }
//=============end of hcp education==========         
         
         
       }
   
     }
     
     
}
/*
else{
    if(system.trigger.isdelete){
       hcpeducation__c hcpedd=[select TotalNumberoftargetHCP__c,id,totalarrived__c,recordtypeid from hcpeducation__c where id=:trigger.new[0].hcpeducation__c];
       if( trigger.new[0].recordtypeid == hpsid )   
       List<CityNameList__c> cnl=[select attendee__c,id from CityNameList__c where Id =: trigger.old[0].CityNameLists__c]; //把对应的城市名单找出来，提取ID,PLANNEDATTENDEE
       List<regionnamelist__c> rgl=[select id,regionAttendee__c from regionnamelist__c where id =: trigger.old[0].RegionNameList__c];
       List<HCPEducation__c> hcpl=[select LocalActivityAttends__c,TotalNumberoftargetHCP__c,id,date__c from HCPEducation__c where id=: trigger.old[0].HCPEducation__c];
       try{
       if(cnl.size() > 0){
           if(cnl.get(0).attendee__c > 0)
               cnl.get(0).attendee__c --;
           update cnl.get(0);
       }
       if(rgl.size() > 0){
           if(rgl.get(0).regionAttendee__c > 0)
               rgl.get(0).regionAttendee__c --;
           update rgl.get(0);
       }
       if(hcpl.size() > 0){
           if(hcpl.get(0).LocalActivityAttends__c > 0)
               hcpl.get(0).LocalActivityAttends__c --;
           update hcpl.get(0);
       }
        }catch(exception e){
        
        }
    }
}     
*/   
       
}